import dt from 'moment';
import lodash from 'lodash';
import log from '../../../log';
import * as data from './data';
import * as queTempCore from '../queTemplate/core';
import * as util from '../../../util';

const likeParms = 'name'.split(',');
// 获取用户创建的问卷数量和回答的问卷数量
function getQueAndQueTempCount(user) {
    return data.getCountByUserId(user.id).then(queCount => {
        return queTempCore.getCountByUserId(user).then(queTempCount => {
            return {queCount, queTempCount};
        });
    });
}
// 获取用户所属所有问卷列表
function getQuesByUserId(page, pageSize, query, user) {
    return data.getQuesByUserId(page, pageSize, query, user).then(data => {
        data.forEach(d => { d.createTime = util.formatDateTime(d.createTime); });
        /*data.sort((a, b) => {
            if (a.createTime < b.createTime) return -1;
            else if (a.createTime === b.createTime) return 0;

            return 1;
        });*/
        return util.data(data, 1);
    });
}

function find(query) {
    return util.resolve().then(() => {
        log.info('find ques params----', query);
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.find(where, 1).then(res => {
            log.info('find ques result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('find ques is err----', err);
        return err;
    });
}
export function add(model, user) {
    return util.resolve().then(() => {
        log.info('add questionnaires  params----', model);
        const questionnaires = {};
        const now = dt().format('YYYY-MM-DD HH:mm:ss');
        questionnaires.id = util.uuid();
        questionnaires.createUserId = user.id;
        questionnaires.createUserName = user.name;
        questionnaires.createTime = now;
        questionnaires.status = 1;
        questionnaires.queTempId = model.queTempId;
        questionnaires.submitTime = now;
        log.info('add questionnaires  parms----', questionnaires);
        return data.add(questionnaires).then(res => {
            log.info('add questionnaires  result----', util.data(res));
            return questionnaires.id;
        });
    }).catch(err => {
        log.error('add questionnaires is err----', err);
        return err;
    });
}
export function edit(model) {
    return util.resolve().then(() => {
        log.info('edit questionnaires  params----', model);
        const questionnaires = {};
        lodash.assign(questionnaires, model);
        questionnaires.queTempId = model.queTempId;
        return data.edit(questionnaires).then(res => {
            log.info('edit questionnaires  result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('edit questionnaires is err----', err);
        return err;
    });
}
export {getQuesByUserId, getQueAndQueTempCount, find};
